package com.jackson0714.passjava.member.config;

import feign.Feign;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
  * @Description: okhttp 配置
  * @Version v1.0.0
  * @author zhangpeng148
  * @date 2022/1/21 10:30
  */
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class OkHttpConfig {

    @Bean
    public OkHttpClient okHttpClient(){
        return new OkHttpClient().newBuilder()
                //设置连接超时
                .connectTimeout(10, TimeUnit.SECONDS)
                //设置读超时
                .readTimeout(10,TimeUnit.SECONDS)
                //设置写超时
                .writeTimeout(10,TimeUnit.SECONDS)
                //设置自动重连
                .retryOnConnectionFailure(true)
                //设置连接池 10个线程 5分钟的长连接
                .connectionPool(new ConnectionPool(10,5L,TimeUnit.MINUTES))
                .build();
    }
}
